package com.aso114.project.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aso114.project.R;
import com.aso114.project.bean.QuestionBean;
import com.aso114.project.bean.QuestionUserListBean;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.activity.ChapterContactActivity;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.treerecyclerview.BaseTreeRVAdapter;
import com.aso114.project.treerecyclerview.ChapterContactOneVH;
import com.aso114.project.treerecyclerview.ChapterContactThreeVH;
import com.aso114.project.treerecyclerview.ChapterContactTwoVH;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2018/12/4.
 */

public class ChapterContactAdapter extends BaseTreeRVAdapter<QuestionUserListBean> {

    private OnItemClick itemClick;

    public void setItemClick(OnItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface  OnItemClick{
        void click(String id);
    }

    @Override
    public void onBindViewHolder(final int type, final QuestionUserListBean questionUserListBean, RecyclerView.ViewHolder holder) {
        switch (type) {
            case 1:
                ChapterContactOneVH oneHolder = (ChapterContactOneVH) holder;
                oneHolder.chapterTitle.setText(questionUserListBean.getSpecialTitle());
                oneHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (questionUserListBean.getChilds() != null && !questionUserListBean.getChilds().isEmpty()) {
                            questionUserListBean.setOpen(!questionUserListBean.isExpand());
                            ArrayList<QuestionUserListBean> childs = (ArrayList<QuestionUserListBean>) questionUserListBean.getChilds();
                            for (int i = 0; i < childs.size(); i++) {
                                QuestionUserListBean a = childs.get(i);
                                a.setParendIsExpand(questionUserListBean.isExpand());
                                //当前实体为最后一个
                                if (i == childs.size() - 1) {
                                    a.setLastIndex(true);
                                    a.setParendLastIndex(true);
                                }
                            }


                            notifyDataSetChanged();
                        }
                    }
                });
                oneHolder.chapterIv.setImageResource(questionUserListBean.isExpand() ? R.mipmap.ej_zk_ic :
                        R.mipmap.ej_yiji_ic);
                oneHolder.chapterRate.setText("正确率：0");
                oneHolder.chapterAnswer.setText("已答题："+questionUserListBean.getRealCount()+"/"+questionUserListBean.getTotalQuestions());
                oneHolder.chapterLineB.setVisibility(questionUserListBean.isExpand() ? View.VISIBLE : View.GONE);
                oneHolder.chapterEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClick!=null){
                            // 传递考卷ID
                            itemClick.click(questionUserListBean.getSpecialId());
                        }
                    }
                });
                break;
            case 2:
                ChapterContactTwoVH twoHolder = (ChapterContactTwoVH) holder;
                twoHolder.chapterTitle.setText(questionUserListBean.getSpecialTitle());
                twoHolder.chapterLineB.setVisibility(questionUserListBean.isParendIsExpand() ? View.VISIBLE : View.GONE);
                twoHolder.chapterLineT.setVisibility(questionUserListBean.isParendIsExpand() ? View.VISIBLE : View.GONE);
                twoHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (questionUserListBean.getChilds() != null && !questionUserListBean.getChilds().isEmpty()) {
                            questionUserListBean.setOpen(!questionUserListBean.isExpand());
                            ArrayList<QuestionUserListBean> childs = (ArrayList<QuestionUserListBean>) questionUserListBean.getChilds();
                            for (int i = 0; i < childs.size(); i++) {
                                QuestionUserListBean a = childs.get(i);
                                a.setParendIsExpand(questionUserListBean.isExpand());
                                //当前实体为最后一个
                                if (i == childs.size() - 1) {
                                    if (questionUserListBean.isParendLastIndex())
                                        a.setLastIndex(true);
                                }
                            }
                            notifyDataSetChanged();
                        }
                    }
                });
                twoHolder.chapterIv.setImageResource(questionUserListBean.isExpand() ? R.mipmap.ej_ejzk_ic :
                        R.mipmap.ej_erji_ic);
                twoHolder.chapterRate.setText("正确率：0");
                twoHolder.chapterAnswer.setText("已答题："+questionUserListBean.getRealCount()+"/"+questionUserListBean.getTotalQuestions());
                //父节点展开且为最后一个
                if (!questionUserListBean.isExpand() && questionUserListBean.isLastIndex()) {
                    twoHolder.chapterLineB.setVisibility(View.GONE);
                } else {

                    twoHolder.chapterLineB.setVisibility(View.VISIBLE);
                }
                twoHolder.chapterEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClick!=null){
                            // 传递考卷ID
                            itemClick.click(questionUserListBean.getSpecialId());
                        }
                    }
                });
                break;
            case 3:
                ChapterContactThreeVH threeHolder = (ChapterContactThreeVH) holder;
                threeHolder.chapterTitle.setText(questionUserListBean.getSpecialTitle());
                threeHolder.chapterLineT.setVisibility(questionUserListBean.isParendIsExpand() ? View.VISIBLE : View.GONE);
                threeHolder.chapterLineB.setVisibility(questionUserListBean.isParendIsExpand() ? View.VISIBLE : View.GONE);
                threeHolder.chapterRate.setText("正确率：0");
                threeHolder.chapterAnswer.setText("已答题："+questionUserListBean.getRealCount()+"/"+questionUserListBean.getTotalQuestions());
                //父节点展开且为最后一个
                if (questionUserListBean.isLastIndex()) {
                    threeHolder.chapterLineB.setVisibility(View.GONE);
                } else {

                    threeHolder.chapterLineB.setVisibility(View.VISIBLE);
                }
                threeHolder.chapterEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClick!=null){
                            // 传递考卷ID
                            itemClick.click(questionUserListBean.getSpecialId());
                        }
                    }
                });
                break;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder vh;
        switch (viewType) {
            case 1:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chapter_contact_item_one, parent, false);
                vh = new ChapterContactOneVH(view);
                break;
            case 2:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chapter_contact_item_two, parent, false);
                vh = new ChapterContactTwoVH(view);
                break;
            default:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chapter_contact_item_three, parent, false);
                vh = new ChapterContactThreeVH(view);
                break;
        }
        return vh;
    }
}
